class Solution(object):
    def maxSatisfaction(self, satisfaction):
        zheng = []
        fu = []
        for num in satisfaction:
            if num >= 0:
                zheng.append(num)
            else:
                fu.append(num)
        zheng.sort()
        ans = 0
        t_sum = 0
        for i in range(len(zheng)):
            ans += (i + 1) * zheng[i]
            t_sum += zheng[i]
        fu.sort(reverse=True)
        fu_sum = 0
        for i in range(len(fu)):
            fu_sum += fu[i]
            if fu_sum + t_sum > 0:
                ans += t_sum + fu_sum
            else:
                break
        return ans

data = Solution()
satisfaction = [-1,-8,0,5,-9]
print(data.maxSatisfaction(satisfaction))